package com.example.blog.controller;

import com.example.blog.constants.Constant;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.PageRequest;
import com.example.blog.model.Result;
import com.example.blog.service.BlogService;
import com.example.blog.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/getCount")
    public Integer queryBlogCount(){
        return blogService.queryBlogCount();
    }

    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList(PageRequest pageRequest){
        if(pageRequest.getCurrentPage()<1){
            return null;
        }
        return blogService.queryBlogList(pageRequest);
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo queryBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("queryBlogDetail blogId:{}",blogId);
        BlogInfo blogInfo = blogService.queryBlogDetail(blogId);
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if(userId!=null&&userId==blogInfo.getUserId()){
            blogInfo.setIsLoginUser(true);
        }else {
            blogInfo.setIsLoginUser(false);
        }
        return blogInfo;
    }

    @RequestMapping("/add")
    public Boolean publishBlog(String title, String content, HttpServletRequest request){
        log.info("publishBlog title:{},content{}",title,content);
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content)){
            log.error("title or content is empty");
            return false;
        }
        String user_token = request.getHeader(Constant.USER_TOKEN_HEADER);
        Integer userId = JwtUtils.getUserIdFromToken(user_token);
        if(userId==null||userId<=0) {
            log.error("用户未登录");
            return false;
        }
        BlogInfo blogInfo = new BlogInfo(title, content, userId);
        Integer result=blogService.publishBlog(blogInfo);
        if(result<1){
            log.error("博客发布失败");
            return false;
        }
        return true;
    }

    @RequestMapping("/update")
    public Boolean update(Integer blogId, String title, String content){
        log.info("update blogId:{},title:{},content:{}",blogId,title,content);
        if(blogId==null || !StringUtils.hasLength(title) || !StringUtils.hasLength(content)){
            log.error("id or title or content is empty");
            return false;
        }
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        Integer result=blogService.updateBlog(blogInfo);
        if(result<1){
            return false;
        }
        return true;
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("delete blogId:{}",blogId);
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer result=blogService.updateBlog(blogInfo);
        if(result<1){
            return false;
        }
        return true;
    }
}
